/*
 * @Author: wuzhouhai wzh200x@126.com
 * @Date: 2024-07-22 17:13:53
 * @LastEditors: wuzhouhai wzh200x@126.com
 * @LastEditTime: 2024-11-29 16:31:59
 * @FilePath: \dwys_web\extensions\phoenix-plugin-client\assets\core\platform\platform.alipay.ts
 * @Description: 
 */
import { ErrorLocalCode } from "../defines/error.local.define";
import { IPlatform } from "../interface/platform.interface";
import { PlatformBase } from "./platform.base";
export class PlatformAlipay extends PlatformBase implements IPlatform {

    login(cb: Function): Promise<string | number | boolean> {
        return new Promise((resolve) => {
            let authCode = false;
            //@ts-ignore
            window['my'].getAuthCode({
                scopes: 'auth_user',
                success(res:any) {
                    if (res.authCode) {
                        //发起网络请求
                        console.log('alipay login back');
                        console.log(res);
                        authCode = res.authCode;
                        cb(authCode);
                        resolve(0);
                    }
                },
                fail(res:any) {
                    console.log('alipay login back fail');
                    console.log(res);
                    cb('');
                    resolve(ErrorLocalCode.PLATFORM_LOGIN_FAIED);
                },
            });
        });
    }
    request(method: string, url: string, data: any, token?: string | undefined): Promise<any> {
        return new Promise((resolve, reject) => {
            if (token) {
                const requetUrl:string = `${this.urlBase}${url}`;
                let reqData = {
                    url: requetUrl,
                    method: method,
                    data: JSON.stringify(data),
                    dataType: 'json',
                    success:(res:any)=>{
                        console.log("AlipayPlatform=>AlipayPlatform res", res);
                        resolve(res.data);
                    },
                    headers:{
                        'content-type': "application/json;charset=utf-8",
                        'Authorization': `Bearer ${token}`
                    },
                    fail:(res:any)=>{
                        console.log("AlipayPlatform=>AlipayPlatform res", res);
                        reject({ code: res.data.code ?? ErrorLocalCode.REQUEST_FAILED, message: res.data });
                    },
                }
                //@ts-ignore
                window['my'].request(reqData);
            } else {
                const requetUrl:string = `${this.urlBase}${url}`;
                let reqData = {
                    url: requetUrl,
                    method: method,
                    data: JSON.stringify(data),
                    dataType: 'json',
                    success:(res:any)=>{
                        console.log("AlipayPlatform=>AlipayPlatform res", res);
                        resolve(res.data);
                    },
                    headers:{
                        'content-type': "application/json;charset=utf-8",
                    },
                    fail:(res:any)=>{
                        console.log("AlipayPlatform=>AlipayPlatform res", res);
                        reject({ code: res.data.code ?? ErrorLocalCode.REQUEST_FAILED, message: res.data });
                    },
                }
                //@ts-ignore
                window['my'].request(reqData);
            }
        });
    } 
}